﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Business;
using blackhouse.JavaScript.JQuery;

namespace blackhouse.BlackApart.Web.Modules {
	public partial class ApartmentsPromotionBanner : System.Web.UI.UserControl {

		#region Fields

		private BlackApart.Business.Company.ApartmentPromotionBaner[] banners;

		#endregion

		#region Event Handlers

		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			this.rptBanners.ItemDataBound += new RepeaterItemEventHandler(rptBanners_ItemDataBound);
			this.SetInstance();
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);
			this.AddJavascript();
			this.PopulateControls();
		}

		void rptBanners_ItemDataBound(object sender, RepeaterItemEventArgs e) {
			switch (e.Item.ItemType) {
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
					BlackApart.Business.Company.ApartmentPromotionBaner banner = e.Item.DataItem as BlackApart.Business.Company.ApartmentPromotionBaner;
					HyperLink lnkApartment = e.Item.FindControl("lnkApartment") as HyperLink;
					Literal ltlText = e.Item.FindControl("ltlText") as Literal;
					if (banner != null) {
						if (lnkApartment != null) {
							lnkApartment.NavigateUrl = UrlUtils.GetApartmentTypeUrl(Factory.Instance().GetPreFolderEvaluation(), banner.ApartmentGroup, DisplayApartmentTypeView.Default, 1);
							lnkApartment.ImageUrl = UrlUtils.ApartmentBannerImageUrl(banner.BanerLink);
							lnkApartment.ToolTip = banner.ApartmentGroup.Name;
						}
						if (ltlText != null)
							ltlText.Text = banner.Text;
					}
					break;

				default:
					break;
			}
		}

		#endregion

		#region Methods

		private void SetInstance() {
			this.banners = Factory.Instance().GetCompany().GetBanners();
		}

		private void PopulateControls() {
			if (this.banners.Length > 0) {
				this.rptBanners.DataSource = this.banners;
				this.rptBanners.DataBind();
			}
			else
				this.rptBanners.Visible = false;
		}

		private void AddJavascript() {
			JQueryLibraries jql = new JQueryLibraries();
			jql.IncludeJQueryRotator();
			jql.IncludeFileBasedOnJQuery("blackhouse.BlackApart.Web.JavaScript.baner.js, blackhouse.BlackApart.Web");
		}

		#endregion

	}
}